package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.Comment;
import com.ycxy.sh.transaction.dao.entity.Commodity;
import com.ycxy.sh.transaction.dao.entity.Notices;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.CommentMapper;
import com.ycxy.sh.transaction.mapper.CommodityMapper;
import com.ycxy.sh.transaction.mapper.NoticesMapper;
import com.ycxy.sh.transaction.service.CommentService;
import com.ycxy.sh.transaction.service.CommodityService;
import com.ycxy.sh.transaction.service.NoticesService;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  评论 服务类
 * </p>
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private NoticesMapper noticesMapper;

    /**插入评论*/
    public void insertComment(Comment comment, HttpSession session){
        log.debug("正在处理插入评论业务...");
        //获取用户id
        Long commentUserId = (Long) session.getAttribute("userid");
        //获取评论内容
        String commentContent = comment.getCommentContent();

        if (StringUtils.isEmpty(commentUserId)) {
           throw new ServiceException(ServiceCode.ERR_FORBIDDEN,"请登录后再评论");
        }
        commentContent = commentContent.replace("<", "&lt;");
        commentContent = commentContent.replace(">", "&gt;");
        commentContent = commentContent.replace("'", "\"");

        comment.setCommentId(Long.valueOf(KeyUtil.genUniqueKey()))
                .setCommentUserId(commentUserId)
                .setCommentContent(commentContent);
        /**插入评论*/
        int row = commentMapper.insert(comment);
        if (row != 1){
            /**发出评论通知消息*/
            Commodity commodity = commodityMapper.selectById(new Commodity().setCommodityId(comment.getCommodityId()));
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey()))
                    .setUserId(comment.getCommodityPublishUserId()).setNoticeType("评论")
                    .setNoticeContent("您的商品 <a href=/product-detail/"+comment.getCommodityId()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommodityName()+"</a> 被评论了，快去看看吧。");
            //插入评论
            int temp = noticesMapper.insert(notices);
            if(temp!=1){
                String message="评论失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            }
           log.debug("成功添加一条评论");
        }
    }
//    /**查询评论*/
//    public List<Comment> queryComments(Long commid){
//        return commentMapper.selectComments(commid);
//    }
//    /**查询评论中用户信息*/
//    public Comment queryById(Long commentid){
//        return commentMapper.selectById(commentid);
//    }
//    /**删除评论*/
    public void deleteComment(Long commentid){
        //return commentMapper.delete(commentid);
    }
}
